﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using My.Data.BusinessObject;
using My.Data.DataAccess;
using My.Data.Factories;
using Gui;

namespace NHibernate.App
{
    public partial class MainFormStudent : Form
    {
        private IPersonDao personDao = DataAccessFactory.LoadPersonDao();
        private static MainFormStudent instance_ = null;
        private int userId_;

        public MainFormStudent(int id)
        {
            userId_ = id;
            InitializeComponent();
        }

        public static MainFormStudent getInstance(int id)
        {
            if (null == instance_)
            {
                instance_ = new MainFormStudent(id);
            }
            return instance_;
        }

        private void MainFormGuest_Load(object sender, EventArgs e)
        {
            IBridge bridge = new StudentBridge();
            AbstractBridge abBridge = new AbstractBridge(bridge);
            abBridge.CallFillTable(mainTable);
        }

        private void SignBtn_Click(object sender, EventArgs e)
        {
            IBridge bridge = new StudentBridge();
            AbstractBridge abBridge = new AbstractBridge(bridge);
            abBridge.CallSignInCourse(userId_);
        }
      
        public void signInCourse()
        {
            ITransaction transaction = null;
            ISession session = ISessionManager.Instance.CreateISession();
            transaction = session.BeginTransaction();


            Person newPerson = new Person();
            newPerson = personDao.GetById(userId_);

            TL1 newTL1 = new TL1();
            newTL1.personTL1 = newPerson;

            session.Save(newTL1);
            transaction.Commit();
            session.Close();
        }

        int close = 0;
        private void MainFormStudent_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (close == 0)
            {
                if (MessageBox.Show("close the program?", "Continue?", MessageBoxButtons.YesNo) == DialogResult.Yes)
                {
                    close = 1;
                    Application.Exit();
                }
                else
                {
                    e.Cancel = true;
                }
            }
        }
    }
}